package cn.psvmc.cxzapi.config.mybatisplus.fieldfill;

import cn.psvmc.cxzapi.util.ToolUtils;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * @Description 自动填充
 **/
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Resource
    private HttpServletRequest request;

    @Override
    public void insertFill(MetaObject metaObject) {
        List<TableFieldInfo> fieldList = TableInfoHelper.getTableInfo(metaObject.getOriginalObject().getClass()).getFieldList();
        long count = fieldList.stream().filter(x -> x.getColumn().equals("createid")).count();

        if (count > 0) {
            //如果有值直接使用，无值取header中的值
            Object createid = this.getFieldValByName("createid",metaObject);
            if(ToolUtils.isEmpty(createid) || "0".equals(ToolUtils.objToString(createid))){
                this.strictInsertFill(metaObject, "createid", Integer.class, ToolUtils.getHeaderUserId(request));
            }
        }
        // 类型和字段必须全部匹配到，才能设置值
        long countUpdate = fieldList.stream().filter(x -> x.getColumn().equals("updateid")).count();
        if (countUpdate > 0) {
            Object createid = this.getFieldValByName("updateid",metaObject);
            if(ToolUtils.isEmpty(createid) || "0".equals(ToolUtils.objToString(createid))){
                this.strictInsertFill(metaObject, "updateid", Integer.class, ToolUtils.getHeaderUserId(request));
            }
        }

        // 类型和字段必须全部匹配到，才能设置值
        this.strictInsertFill(metaObject, "createtime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updatetime", LocalDateTime.class, LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        List<TableFieldInfo> fieldList = TableInfoHelper.getTableInfo(metaObject.getOriginalObject().getClass()).getFieldList();

        long countUpdate = fieldList.stream().filter(x -> x.getColumn().equals("updateid")).count();
        if (countUpdate > 0) {
            Object createid = this.getFieldValByName("updateid",metaObject);
            if(ToolUtils.isEmpty(createid) || "0".equals(ToolUtils.objToString(createid))){
                this.strictInsertFill(metaObject, "updateid", Integer.class, ToolUtils.getHeaderUserId(request));
            }
        }

        this.strictUpdateFill(metaObject, "updatetime", LocalDateTime.class, LocalDateTime.now());
    }

    @Override
    public MetaObjectHandler strictFillStrategy(MetaObject metaObject, String fieldName, Supplier<?> fieldVal) {
        // 去除空判断
        Object obj = fieldVal.get();
        if (Objects.nonNull(obj)) {
            metaObject.setValue(fieldName, obj);
        }
        return this;
    }


}
